package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/model"
	"shop-bean/request"
	"shop-common/library/serializer"
	"shop-common/utils"
	"shop-common/utils/contextArgs"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
	"shop-service/business"
)

type PickAddrController struct {
}

// @Tags 自提点管理
// @description: 分页
// @Summary 分页
// @Security ApiKeyAuth
// @function: Page
// @Accept  json
// @Produce  json
// @Param current query string false "当前页，默认1"
// @Param size query string false "每页大小，默认10"
// @Param keyword query string false "keyword"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/pickAddr/page [get]
func (p *PickAddrController) Page(ctx *gin.Context) {
	form := pagehelper.SearchPageRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	paginator := pagehelper.New().SetOffset(&form).IsPush(form.Keyword != "", queryWrapper.Eq("addr_name", form.Keyword)).Paginator()
	page, err := business.ServiceImpl().PickAddrService.FindPage(ctx, paginator)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", page)
}

// @Tags 自提点管理
// @description: 新增
// @Summary 新增
// @Security ApiKeyAuth
// @function: Save
// @Accept  json
// @Produce  json
// @Param addrName query string true "自提点名称"
// @Param addr query string true "详细地址"
// @Param area query string true "区"
// @Param areaId query int true "区ID"
// @Param city query string true "城市"
// @Param cityId query int true "城市ID"
// @Param province query string true "省份"
// @Param provinceId query int true "省份"
// @Param mobile query string true "手机号"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/pickAddr [post]
func (p *PickAddrController) Save(ctx *gin.Context) {
	form := request.PickAddrRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	shopId, err := contextArgs.GetSHopId(ctx)
	if err != nil {
		serializer.Fail(ctx, "无权限")
		return
	}

	var addr model.PickAddr
	err = utils.StructMapStrict(form, &addr)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicCreateErr)
		return
	}
	addr.ShopId = shopId

	err = business.ServiceImpl().PickAddrService.Create(ctx, &addr)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicCreateErr)
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 自提点管理
// @description: 修改
// @Summary 修改
// @Security ApiKeyAuth
// @function: Update
// @Accept  json
// @Produce  json
// @Param data body request.PickAddrRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/pickAddr [put]
func (p *PickAddrController) Update(ctx *gin.Context) {
	form := request.PickAddrRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if form.AddrId < 1 {
		serializer.Fail(ctx, "自提点Id不能为空")
		return
	}

	var addr model.PickAddr
	utils.StructMap(form, &addr)
	err := business.ServiceImpl().PickAddrService.Updates(ctx, form.AreaId, &addr)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 自提点管理
// @description: 删除
// @Summary 删除
// @Security ApiKeyAuth
// @function: Delete
// @Accept  json
// @Produce  json
// @Param data body request.CommonBatchRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/pickAddr [delete]
func (p *PickAddrController) Delete(ctx *gin.Context) {
	form := request.CommonBatchRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	err := business.ServiceImpl().PickAddrService.DeleteBatch(ctx, form.Ids)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicDeleteErr)
		return
	}

	serializer.Success(ctx, "")
}
